package com.newsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.NewsCoverMapper;
import com.newsystem.mapper.NewsMapper;
import com.newsystem.pojo.News;
import com.newsystem.pojo.NewsCover;
import com.newsystem.service.impl.NewsCoverService;
import com.newsystem.service.impl.NewsService;
import freemarker.template.utility.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@RestController
@RequestMapping("/news")
@RequiredArgsConstructor
public class NewsController {

    private final NewsMapper newsMapper;
    private final NewsService newsService;
    private final NewsCoverMapper newsCoverMapper;
    private final NewsCoverService newsCoverService;

    //todo mediumId修改成了userId
    @ApiOperation("发布新闻视频")
    @PostMapping("/addVideoNews")
    public CommonResult<Object> addVideoNews(String userId,String content,String videoUrl,String title,String coverUrl){
        return newsService.insertVideoNews(userId,content,videoUrl,title,coverUrl);
    }

    //todo mediumId修改成了userId
    @ApiOperation("发布文本新闻")
    @PostMapping("/addTextNew")
    public CommonResult<Object> addTextNew(String userId,String content,String title,String coverUrl){
        return newsService.insertTestNews(userId,content,title,coverUrl);
    }

    //todo mediumId修改成了userId
    @ApiOperation("修改文本新闻内容")
    @PutMapping("/updateTextNew")
    public CommonResult<Object> updateTextNew(String userId,String newsId,String content){
        return newsService.updateTextNew(userId,newsId,content);
    }

    @ApiOperation("修改新闻封面")
    @PutMapping("/updateNewCover")
    public CommonResult<Object> updateNewCover(String newsId,String coverUrl,String title){
        return newsCoverService.updateNewCover(newsId,coverUrl,title);
    }


    //todo mediumId修改成了userId
    @ApiOperation("媒体批量删除新闻")
    @DeleteMapping("/deleteNews")
    public CommonResult<Object> deleteNews(String userId, @RequestBody List<String > newsId){
        return newsService.deleteNews(userId,newsId);
    }

    @ApiOperation("根据获取新闻封面列表")
    @GetMapping("/getNewCoverList")
    public CommonResult<Object> getNewCoverList(Integer startPage,Integer sumPage){
        return newsCoverService.selectCoverList(startPage,sumPage);
    }

    @ApiOperation("根据新闻id获得新闻内容")
    @GetMapping("/getNewInfo")
    public CommonResult<Object> getNewInfo(String newsId){
        return newsService.selectNewsInfo(newsId);
    }

    @ApiOperation("模糊查询返回新闻的内容和id")
    @GetMapping("/search")
    public CommonResult<Object> searchNews(String context){
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("content",context);
        queryWrapper.select("id","content");
        List<News> news = newsMapper.selectList(queryWrapper);
        if(news==null){
            return CommonResult.failed("模糊查询出错");
        }
        return CommonResult.success(news);
    }

}

